package com.fingard.dsp.bank.directbank.weixin01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IMiniProgImag;
import com.fingard.dsp.bank.reqrespobj.mimiprogimag.ReqMiniProgImag;
import com.fingard.dsp.bank.reqrespobj.mimiprogimag.RetAtsMiniProgImag;
import com.fingard.text.StringHelper;
import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class WEIXIN01MiniProgImag extends WEIXIN01Base implements IMiniProgImag {
    @Override
    public RetAtsMiniProgImag getMiniProgImag(ReqMiniProgImag p_req) throws Exception {
        RetAtsMiniProgImag retAts = new RetAtsMiniProgImag();

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "unifiedorder");
            if (actItem != null) {
                // 1.获取access_token
                String accessTokenUrl = bankFront.getUrl("miniAccessToken");
                if (StringHelper.isNullOrEmpty(accessTokenUrl)) {
                    retAts.respCode = "0";
                    retAts.respInfo = "小程序码access_token地址为空,请检查BankFront.xml中的'miniAccessToken'配置!";
                    return retAts;
                }
                WriteBankLogLn("发送微信小程序码accessToken服务地址:" + accessTokenUrl);
                accessTokenUrl += "?grant_type=client_credential&appid=" + actItem.appId + "&secret=" + actItem.appSecret;
                WriteBankLogLn("拼接参数后发送微信小程序码accessToken服务地址:" + accessTokenUrl);
                String[] tmpStrRet = sendPost(null, accessTokenUrl);
                if (tmpStrRet[0].length() == 0) {
                    JSONObject respJson = JSONObject.parseObject(tmpStrRet[1]);
                    String tmpRespCode = respJson.getString("errcode");
                    if ("0".equalsIgnoreCase(tmpRespCode)) {
                        String accessToken = respJson.getString("access_token");
                        WriteBankLogLn("accessToken获取成功,值为:" + accessToken);

                        // 2.获取微信小程序码
                        JSONObject tmpReqJson = new JSONObject();
                        tmpReqJson.put("access_token",accessToken);
                        tmpReqJson.put("scene",p_req.rdSeq);
                        tmpReqJson.put("page",p_req.page);
                        tmpReqJson.put("width","");
                        tmpReqJson.put("auto_color","");
                        tmpReqJson.put("line_color","");
                        tmpReqJson.put("is_hyaline","");

                        String miniProgImagUrl = bankFront.getUrl("miniProgImag");
                        if (StringHelper.isNullOrEmpty(miniProgImagUrl)) {
                            retAts.respCode = "0";
                            retAts.respInfo = "小程序码服务地址为空,请检查BankFront.xml中的'miniProgImag'配置!";
                            return retAts;
                        }
                        WriteBankLogLn("发送微信小程序码服务地址:" + miniProgImagUrl);
                        miniProgImagUrl += "?access_token=" + accessToken;
                        WriteBankLogLn("拼接参数后发送微信小程序码服务地址:" + miniProgImagUrl);
                        String[] tmpRetStr = sendPost(JSON.toJSONString(tmpReqJson), miniProgImagUrl);
                        if (tmpRetStr[0].length() == 0) {
                            try {
                                JSONObject miniRespJson = JSONObject.parseObject(tmpRetStr[1]);
                                retAts.respCode = "-1";
                                retAts.respInfo = miniRespJson.getString("errcode") + "-" + miniRespJson.getString("errmsg");
                            } catch(Exception ex) {
                                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tmpRetStr[1].getBytes(getCharset()));
                                InputStream inputStream=(InputStream)byteArrayInputStream;
                                BufferedImage image = ImageIO.read(inputStream);
                                ByteArrayOutputStream os=new ByteArrayOutputStream();
                                ImageIO.write(image, "png", os);
                                retAts.respCode = "0";
                                retAts.respInfo = "成功";
                                retAts.image = new String(new Base64().encode(os.toByteArray()));
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpStrRet[1];
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = respJson.getString("errmsg");
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}